<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>shelf console</title>
<link rel=StyleSheet href="shelfconsole.css" type="text/css">
</head>
<body onclick='focusinput()'>
<div id='consolewidget'>
<div id='console'>
Shelf v0.1a<br>
This is the Shelf console.<br>
</div>
<form name='promptform' action='javascript:handleInput()'>
<div id='promptname'>about:blank@shelf$<input type=text name='inputprompt' autocomplete='off' onkeyup="getlink(this.value)"></input></div> 
</form>
</div>
<div id='textnotifications'>
</div>
<div id='notifications'>
</div>
<div id='shortcuts'>
<div id='hidebutton'><img src='tango_console_hide.png' onclick = 'hideshow("hide")'></div>
</div>
</body>
<script type="text/javascript">
window.onfocus=focusinput();
window.onmouseup=focusinput();
var uri = "invalid";
var promptstring = "about:blank@shelf$";
setPromptName("about:blank");

//this version of getlink does not write to the statusbar, but returns its statustext instead
function getlink2(string)
{
  if(string == "")
  {
      uri = "invalid";
      return "";
  }
  var searchstring = string;
  //if(string.indexOf(" "))
  //{
  //  return "<i>no spaces allowed in URI</i>";
  //}
  var usage = isCommand(searchstring);
  if(usage != "invalid")
  {
    uri = "invalid";
    return("<i>"+usage+"</i>")
  }
  if(searchstring.indexOf(":/") > 2)
  {
    uri = searchstring;
    return ("<i>"+uri+"</i>");
  }
  uri = isURI(searchstring)
  if(uri != "invalid")
  {
    return ("<i>"+uri+"</i>");
  }

  //if we get this far, it means the uri was not recognised so we construct one of our own
  uri = "http://" + searchstring;
  if(uri.indexOf(".") < uri.length - 4)
  {
    uri += ".com";
  }
  return ("<i>"+uri+"</i>");
}

function getlink(string)
{
  if(string == ""){uri = "invalid";return;}
  var searchstring = string;
  if(searchstring.indexOf(" ")>1)
  {
    var line = searchstring.split(" ");
    if(line[0] == 'new')
    {
      statusPrint("open "+getlink2(line[1])+" in new shelf");
    }
    else
    {
      statusPrint("");
    }
  }
  else
  {
    statusPrint(getlink2(searchstring));
  }
/*
  if(string.indexOf(" "))
  {
    var line = string.split(" ");
    searchstring = line[0];
  }
  var usage = isCommand(searchstring);
  if(usage != "invalid")
  {
    statusPrint("<i>"+usage+"</i>");
    document.promptform.inputprompt.style.fontWeight = "bolder";
    uri = "invalid";
    if(searchstring == 'new')
    {
      if(line.length > 1)
      {
        uri = isURI(line[1]);
        statusPrint("open <i>"+uri+"</i> in new shelf");
      }
    }
    return;
  }
  else
  {
    if(searchstring.indexOf("http://") == 0)
    {
      statusPrint("<i>"+searchstring+"</i>");
      uri = searchstring;
      return;
    }
    uri = isURI(searchstring);
    if(uri != "invalid")
    {
      statusPrint("<i>"+uri+"</i>");
      return;
    }
    else
    {
       //construct url from string
      uri = "http://" + searchstring;
      if(searchstring.indexOf(".") == -1)
      {
        uri += ".com";
      }
      statusPrint("<i>"+uri+"</i>");
      return;
    }
  }
  document.promptform.inputprompt.style.fontWeight = "normal";
  statusPrint("");
*/
}

function addNotification(notification)
{
  var notificationArea = document.getElementById('notifications');
  notificationArea.innerHTML += notification;
}
function clearNotifications()
{
  var notificationArea = document.getElementById('notifications');
  notificationArea.innerHTML = "";
}
function hideshow(hide)
{
  var hidebutton = document.getElementById('hidebutton');
  if(hide == "hide")
  {
    hidebutton.innerHTML = "<img src='tango_console_show.png' onclick = 'hideshow(" + '"unhide"' +")'>";
    hideConsole(hide);
  }
  else
  {
    hidebutton.innerHTML = "<img src='tango_console_hide.png' onclick = 'hideshow(" + '"hide"' +")'>";
    hideConsole(hide);
  }
}
function consolePrint(string)
{
  var console = document.getElementById('console');
  console.innerHTML += string + '<br>';
}
function statusPrint(string)
{
  var console = document.getElementById('textnotifications');
  console.innerHTML = string;
}
function setPromptName(string)
{
  promptstring = string + "@shelf$";
  var prompt = document.getElementById("promptname");
  prompt.innerHTML = promptstring + "<input type=text name='inputprompt' autocomplete='off' onkeyup="+'"getlink(this.value)"'+"></input>";
}
function handleInput()
{
  document.promptform.inputprompt.style.fontWeight = "normal";
  statusPrint("");
  //var promptstring = 'armen@shelf$';
  var inputstring = document.promptform.inputprompt.value;
  var printstring = promptstring + ' ' + inputstring;
  consolePrint(printstring);
  if(!isNaN(inputstring*1))
  {
    consolePrint(switchShelf(inputstring));
    refocusAndScroll();
    return;
  }
  var command=inputstring.split(" ");

  if(command[0] == "plug")
  {
    consolePrint(loadPlugin(command[1]));
    refocusAndScroll();
    return;
  }
  if(command[0] == "setsize")
  {
    consolePrint(setConsoleSize(command[1]));
    refocusAndScroll();
    return;
  }
  if(command[0] == 'ls')
  {
    consolePrint(listShelves());
    refocusAndScroll();
    return;
  }
  if(command[0] == 'info')
  {
    consolePrint(shelfInfo());
    refocusAndScroll();
    return;
  }
  if(command[0] == 'new')
  {
    newShelf(uri);
    refocusAndScroll();
    return;
  }
  if(command[0].indexOf("http://") == 0)
  {
    consolePrint(openInShelf(command[0]));
    refocusAndScroll();
    return;
  }
  else 
  {
    if(uri != 'invalid')
    {
      consolePrint(openInShelf(uri));
    }
  }
  refocusAndScroll();
  return;
}

function refocusAndScroll()
{
  document.promptform.inputprompt.value = "";
  document.promptform.inputprompt.focus();
  var console = document.getElementById('consolewidget');
  console.scrollTop = console.scrollHeight;
}
function focusinput(e)
{
  if(typeof document.promptform != 'undefined')
  {
    document.promptform.inputprompt.focus();
  }
}
</script>
</html>